capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local crime "";
local output "";

/* 
Complier prep (for Figs B1, B9)
*/

local bwidth = 264 ;
local demo_list "all black singno twopar parhigh parlow male female othmen intel phys white nonment nonw " ; 

foreach demo in `demo_list' { ;

use "`crime'\prep2\crime_prwora_prepforreg.dta", clear ;
merge 1:1 hun using "`crime'\prep2\crime_prwora_prepforreg_exactage.dta", keepusing(n_crime2any_pre18) ;

keep if dobdd_run <= `bwidth' & keep_1997_2017_2c == 1 ; 

/* Subsamples */
gen demo_all = 1 ;
gen byte demo_othment	= (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
gen byte demo_nonment 	= (demo_othment == 0) ;
gen byte demo_intel 	= (n_diag1_mental == 1 & (substr(dibdig,1,3) == "317" | substr(dibdig,1,3) == "318" | substr(dibdig,1,3) == "319" )) ;
gen byte demo_phys 		= (n_diag1_mental != 1) ;
gen byte demo_twopar 	= (n_fam_singmom != 1 & n_fam_nopar != 1) ;
gen byte demo_singno 	= (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
gen byte demo_nopar  	= (n_fam_nopar == 1) ;
gen byte demo_male 	 	= (n_male ==1) ;
gen byte demo_female 	= (n_male !=1) ;
gen byte demo_white		= (race == "W") ;
gen byte demo_black 	= (race == "B") ;
gen byte demo_nonw		= (race != "W") ;

foreach race in white black nonw { ;
	foreach dis in intel phys othment { ;
		gen byte demo_`race'_`dis' = (demo_`race' == 1 & demo_`dis' == 1) ;
	} ;
} ;

egen cnty_ue97 = rowmean(cnty_urate_1997-cnty_urate_1998) ;
egen cnty_uegr = rowmean(cnty_urate_2007-cnty_urate_2012) ;
egen cnty_ueall = rowmean(cnty_urate_1997-cnty_urate_2017) ;
rename cnty_crimerate cnty_crime ;
rename cnty_share_blue cnty_dem ;
rename cnty_pop2000 cnty_pop ;

foreach cnty in crime dem pop ue97 uegr ueall { ;
	summ cnty_`cnty', det ;
	gen byte demo_`cnty'high = (cnty_`cnty' > r(p50) & cnty_`cnty' != .) ;
	gen byte demo_`cnty'low = (cnty_`cnty' <= r(p50) & cnty_`cnty' != .) ;
	replace demo_`cnty'high = . if cnty_`cnty' == . ;
	replace demo_`cnty'low = . if cnty_`cnty' == . ;
	tab demo_`cnty'high, m ;
	tab demo_`cnty'low, m ;
} ;

summ n_earnhhpre9095, det ;
replace n_earnhhpre9095 = 0 if n_earnhhpre9095 == . ;
gen byte demo_parhigh = (n_earnhhpre9095 > r(p50) & n_earnhhpre9095 != .) ;
gen byte demo_parlow = (n_earnhhpre9095 <= r(p50) & n_earnhhpre9095 != .) ;
replace demo_parhigh = . if n_earnhhpre9095 == . ;
replace demo_parlow = . if n_earnhhpre9095 == . ;
tab demo_parhigh, m ;
tab demo_parlow, m ;

gen byte demo_record = (n_crime2any_pre18 == 1) ;

gen byte firstage_lt6 = (n_firstage <=6) ;
gen byte firstage_7to13 = (n_firstage > 6 & n_firstage <=13) ;
gen byte firstage_gt13 = (n_firstage > 13 & n_firstage < .) ;

/* Specific diagnoses */
local dibdig_list "" ;
levelsof dibdig, local(digs) ;
foreach dig in `digs' { ;
	gen byte dig_`dig' = (dibdig == "`dig'") ;
	summ dig_`dig' ;
	if r(mean) > .01 { ; 
		local dibdig_list "`dibdig_list' dig_`dig'" ;
	} ;
} ;

destring dibdig, replace ;
gen byte n_diag1_intel = (dibdig >= 3170 & dibdig <= 3190) ;



/* ******************************************* */
/* **** COMPLIER CHARACTERISTICS ANALYSIS **** */
/* ******************************************* */

keep if demo_`demo' == 1 ;

local char_list "demo_* n_diag1_* n_dibmdr_mie n_dibmdr_mip n_dibmdr_mine n_dibmdr_miss firstage_* `dibdig_list'" ;

gen byte sample_all = 1 ;
gen byte sample_mip = (n_dibmdr_mip == 1) ;

gen stay = 1-n_age18unfav;

local compliers_lab = "" ;
foreach var of varlist 
			`char_list'
	{;
		local compliers_lab = "`compliers_lab' `var'" ;
		local total = `total' + 1 ;
	};
	
	foreach gp in always never comp { ;
		local compliers_lab = "`compliers_lab' `gp'" ;
		local total = `total' + 1 ;
	} ;

matrix compliers=J(5,`total',.) ;
local row = 0 ;
local total = 0 ;

foreach sample in all mip { ;
	
	local row = `row' + 1 ;
	local column`row' = 0 ;

	/* ESTIMATE PROPORTION OF ALWAYS TAKERS */
	/* Among birthday before cutoff (instrument off), how many are still removed */
	summ n_age18unfav if !n_post & sample_`sample' == 1 ;
	scalar define p_always_`sample' = r(mean) ;

	/* ESTIMATE PROPORTION OF NEVER TAKERS */
	/* Among birthday after cutoff (instrument on), how many are NOT removed */
	summ stay if n_post & sample_`sample' == 1 ;
	scalar define p_never_`sample' = r(mean) ;
	
	/* ESTIMATE PROPORTION OF COMPLIERS */
	/* P(complier) = 1 - P(always taker) - P(never taker) */
	scalar define p_comp_`sample' = 1 - p_always_`sample' - p_never_`sample';

	/* ESTIMATE AVERAGE OF CHARACTERISTIC OVER (after AND REMOVED) SET */
	foreach var of varlist 
			`char_list'
	{;
		summ `var' if n_post & n_age18unfav & sample_`sample' == 1 ;
		scalar define EZ1D1_`var'_`sample'=r(mean);
	};

	/* ESTIMATE AVERAGE OF CHARACTERISTIC OVER (before AND REMOVED) SET */
	foreach var of varlist 
			`char_list'
	{;
		summ `var' if !n_post & n_age18unfav & sample_`sample' == 1 ;
		scalar define EZ0D1_`var'_`sample'=r(mean);
	};

	/* ESTIMATE AVERAGE OF CHARACTERISTICS FOR COMPLIERS */
	foreach var of varlist 
			`char_list'
	{;
		scalar define Ecomp_`var'_`sample'=((p_always_`sample'+p_comp_`sample')/p_comp_`sample')*
									(EZ1D1_`var'_`sample'-((p_always_`sample'/(p_always_`sample'+p_comp_`sample'))*EZ0D1_`var'_`sample'));
	};

	/* ESTIMATE AVERAGES OVER ENTIRE POPULATION */
	foreach var of varlist 
			`char_list'
	{;
		summ `var' if sample_`sample' == 1 ;
		scalar define Epop_`var'_`sample'=r(mean);
	};

	/* CALCULATE RATIO */
	foreach var of varlist 
			`char_list'
	{;
		scalar define ratio_`var'_`sample'=Ecomp_`var'_`sample'/Epop_`var'_`sample';
	};
	
	/* PUT RATIO IN MATRIX */
	foreach var of varlist 
			`char_list'
	{;
		local column`row' = `column`row'' + 1 ;
		pause ;
		matrix compliers[`row',`column`row''] = ratio_`var'_`sample' ;
	};
	
	foreach gp in always never comp { ;
		local column`row' = `column`row'' + 1 ;
		matrix compliers[`row',`column`row''] = p_`gp'_`sample' ;
	} ;

} ;

local row = `row' + 1 ;
foreach var of varlist 
			`char_list'
	{;
		local column`row' = `column`row'' + 1 ;
		summ `var' ;
		matrix compliers[`row',`column`row''] = r(mean) ;
	};


clear ;
svmat compliers ; 
rename (compliers*) (`compliers_lab') ;
gen 	label = "all" 	if _n == 1 ;
replace label = "mip" 	if _n == 2 ;
replace label = "mean" 	if _n == 3 ;
order label ;

save "`output'\prwora_crime_compliers_`demo'.dta", replace ;

} ;

capture log close;
